using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;

namespace BW.Mes.WinForms.Process.OperationModel
{
    // 分页管理器类
    public class PaginationManager
    {
        // 分页属性
        public int CurrentPage { get; private set; } = 1;
        public int PageSize { get; private set; } = 10;
        public int TotalRecords { get; private set; } = 0;
        public int TotalPages { get; private set; } = 1;

        // 控件引用
        private PanelControl paginationPanel;
        private LabelControl lblPageInfo;
        private LabelControl lblTotalRecords;
        private SimpleButton btnFirst;
        private SimpleButton btnPrevious;
        private SimpleButton btnNext;
        private SimpleButton btnLast;
        private ComboBoxEdit cmbPageSize;

        // 分页事件 - 当页码或页大小改变时触发
        public event EventHandler PageChanged;

        // 初始化分页管理器
        public void Initialize(Control parentControl)
        {
            // 检查父控件中是否已存在名为"paginationPanel"的控件
            Control existingPanel = parentControl.Controls["paginationPanel"];
            if (existingPanel != null)
            {
                parentControl.Controls.Remove(existingPanel);
            }
            
            CreatePaginationPanel(parentControl);
            UpdatePaginationInfo();
        }

        // 创建分页面板和控件
        private void CreatePaginationPanel(Control parentControl)
        {
            // 创建分页面板
            paginationPanel = new PanelControl();
            paginationPanel.Name = "paginationPanel";
            paginationPanel.Dock = DockStyle.Bottom;
            paginationPanel.Height = 40;
            paginationPanel.Parent = parentControl;
            paginationPanel.BringToFront();

            // 首页按钮
            btnFirst = new SimpleButton();
            btnFirst.Name = "btnFirst";
            btnFirst.Text = "首页";
            btnFirst.Location = new System.Drawing.Point(10, 8);
            btnFirst.Size = new System.Drawing.Size(60, 25);
            btnFirst.Click += (s, e) => { 
                CurrentPage = 1;
                PageChanged?.Invoke(this, EventArgs.Empty);
            };
            paginationPanel.Controls.Add(btnFirst);

            // 上一页按钮
            btnPrevious = new SimpleButton();
            btnPrevious.Name = "btnPrevious";
            btnPrevious.Text = "上一页";
            btnPrevious.Location = new System.Drawing.Point(80, 8);
            btnPrevious.Size = new System.Drawing.Size(60, 25);
            btnPrevious.Click += (s, e) => {
                if (CurrentPage > 1)
                {
                    CurrentPage--;
                    PageChanged?.Invoke(this, EventArgs.Empty);
                }
            };
            paginationPanel.Controls.Add(btnPrevious);

            // 页码信息标签
            lblPageInfo = new LabelControl();
            lblPageInfo.Name = "lblPageInfo";
            lblPageInfo.Text = "页码: 1 / 1";
            lblPageInfo.Location = new System.Drawing.Point(150, 12);
            lblPageInfo.AutoSize = true;
            paginationPanel.Controls.Add(lblPageInfo);
            
            // 下一页按钮
            btnNext = new SimpleButton();
            btnNext.Name = "btnNext";
            btnNext.Text = "下一页";
            btnNext.Location = new System.Drawing.Point(240, 8);
            btnNext.Size = new System.Drawing.Size(60, 25);
            btnNext.Click += (s, e) => {
                if (CurrentPage < TotalPages)
                {
                    CurrentPage++;
                    PageChanged?.Invoke(this, EventArgs.Empty);
                }
            };
            paginationPanel.Controls.Add(btnNext);

            // 尾页按钮
            btnLast = new SimpleButton();
            btnLast.Name = "btnLast";
            btnLast.Text = "尾页";
            btnLast.Location = new System.Drawing.Point(310, 8);
            btnLast.Size = new System.Drawing.Size(60, 25);
            btnLast.Click += (s, e) => {
                CurrentPage = TotalPages;
                PageChanged?.Invoke(this, EventArgs.Empty);
            };
            paginationPanel.Controls.Add(btnLast);

            // 每页显示标签
            var lblPageSize = new LabelControl();
            lblPageSize.Name = "lblPageSize";
            lblPageSize.Text = "每页显示:";
            lblPageSize.Location = new System.Drawing.Point(390, 12);
            lblPageSize.AutoSize = true;
            paginationPanel.Controls.Add(lblPageSize);

            // 页大小下拉框
            cmbPageSize = new ComboBoxEdit();
            cmbPageSize.Name = "cmbPageSize";
            cmbPageSize.Properties.Items.AddRange(new object[] { 10, 20, 50, 100 });
            cmbPageSize.Location = new System.Drawing.Point(460, 9);
            cmbPageSize.Size = new System.Drawing.Size(60, 24);
            cmbPageSize.EditValue = PageSize;
            cmbPageSize.SelectedIndexChanged += (s, e) => {
                if (cmbPageSize.EditValue != null)
                {
                    PageSize = Convert.ToInt32(cmbPageSize.EditValue);
                    CurrentPage = 1;
                    PageChanged?.Invoke(this, EventArgs.Empty);
                }
            };
            paginationPanel.Controls.Add(cmbPageSize);

            // 总记录数标签
            lblTotalRecords = new LabelControl();
            lblTotalRecords.Name = "lblTotalRecords";
            lblTotalRecords.Text = "共 0 条记录";
            lblTotalRecords.Location = new System.Drawing.Point(550, 12);
            lblTotalRecords.AutoSize = true;
            lblTotalRecords.Visible = true;
            paginationPanel.Controls.Add(lblTotalRecords);
        }

        // 更新分页信息
        public void UpdatePaginationInfo()
        {
            // 确保总页数至少为1
            if (TotalPages == 0) TotalPages = 1;

            // 更新页码信息
            if (lblPageInfo != null)
            {
                lblPageInfo.Text = $"页码: {CurrentPage} / {TotalPages}";
            }
            
            // 更新总记录数
            if (lblTotalRecords != null)
            {
                lblTotalRecords.Text = $"共 {TotalRecords} 条记录";
                lblTotalRecords.Visible = true;
            }

            // 更新按钮状态
            if (btnFirst != null) btnFirst.Enabled = (CurrentPage > 1);
            if (btnPrevious != null) btnPrevious.Enabled = (CurrentPage > 1);
            if (btnNext != null) btnNext.Enabled = (CurrentPage < TotalPages);
            if (btnLast != null) btnLast.Enabled = (CurrentPage < TotalPages);
        }

        // 更新分页数据
        public void UpdatePagingData(int totalRecords)
        {
            TotalRecords = totalRecords;
            TotalPages = (TotalRecords > 0) ? (int)Math.Ceiling(TotalRecords / (double)PageSize) : 1;
            
            // 修正当前页，确保不超过总页数
            if (CurrentPage > TotalPages)
            {
                CurrentPage = TotalPages;
            }
            
            UpdatePaginationInfo();
        }

        // 重置为第一页
        public void ResetToFirstPage()
        {
            CurrentPage = 1;
        }
        
        // 设置当前页码
        public void SetCurrentPage(int pageNumber)
        {
            // 确保页码在有效范围内
            if (pageNumber < 1)
                pageNumber = 1;
            else if (pageNumber > TotalPages)
                pageNumber = TotalPages;
                
            CurrentPage = pageNumber;
            UpdatePaginationInfo();
        }
    }
} 